﻿using System;
using Microsoft.Phone.Tasks;

namespace Tequila.Phone.Sharing.Email
{
    /// <summary>
    /// Email sharing service class.
    /// </summary>
    public class EmailShareService : BaseShareService
    {
        private readonly IFileStorageManager _fileStorage;
        private readonly Func<string, Uri, string> _emailBodyBuilderFunc;

        /// <summary>
        /// Initializes new instance of class.
        /// </summary>
        /// <param name="fileStorage"></param>
        /// <param name="emailBodyBuilderFunc"></param>
        public EmailShareService(IFileStorageManager fileStorage, Func<string, Uri, string> emailBodyBuilderFunc = null)
        {
            _fileStorage = fileStorage;
            _emailBodyBuilderFunc = emailBodyBuilderFunc;
        }

        /// <summary>
        /// Shares <see cref="Article">article</see>.
        /// </summary>
        /// <param name="article"></param>
        public override void Post(Article article)
        {
            var htmlContent = _fileStorage.ReadFile(article.Link);
            var plainContent = htmlContent.TransformHtmlToPlain();

            var content = _emailBodyBuilderFunc == null
                              ? plainContent
                              : _emailBodyBuilderFunc(plainContent, new Uri(article.OriginalLink));

            var emailComposer = new EmailComposeTask
                                    {
                                        To = string.Empty,
                                        Subject = article.Title,
                                        Body = content
                                    };

            emailComposer.Show();
        }

        /// <summary>
        /// When implemented, shares <see cref="GaleryImage">image</see>.
        /// </summary>
        /// <exception cref="NotSupportedException"></exception>
        /// <param name="image"></param>
        public override void PostImage(GaleryImage image)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// When implemented, returns logout url.
        /// </summary>
        /// <exception cref="NotSupportedException"></exception>
        /// <returns>
        /// Type: <see cref="T:System.Uri"/>.
        /// </returns>
        public override Uri GetLogoutUrl()
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// When implemented, creates custom Login uri.
        /// </summary>
        /// <exception cref="NotSupportedException"></exception>
        public override void BuildLoginUri()
        {
            throw new NotSupportedException();
        }
    }
}
